package com.batain.txchart.controller;

import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.batain.common.Entity.Result;
import com.batain.common.utils.FileUploadUtil;
import com.batain.txchart.domain.MediaData;
import com.batain.txchart.service.ITxChatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;


/**
 * 获取会话信息
 * @author batain
 */
@Controller
public class TxCharController {

    Logger logger = LoggerFactory.getLogger(TxCharController.class);

    @Autowired
    private ITxChatService txChatService;


    /**
     * 初始化sdk
     * @param sdkId
     * @param corpid
     * @param secrectkey
     * @return
     */
    @RequestMapping("/txchat/initSdk")
    @ResponseBody
    public Result initSdk(String sdkId,String corpid,String secrectkey){
        return txChatService.initSdk(sdkId,corpid,secrectkey);
    }

    /**
     * 撤销skd
     * @param sdkId
     * @return
     */
    @RequestMapping("/txchat/destroySdk")
    @ResponseBody
    public Result destroySdk(String sdkId){
        return txChatService.destroySdk(sdkId);
    }

    /**
     * 获取会话信息（不解密）
     * @param sdkId
     * @return
     */
    @RequestMapping("/txchat/getChatData")
    @ResponseBody
    public Result getChatData(String sdkId,int seq,int limit,String proxy,String passwd,int timeout){
        return txChatService.getChatData(sdkId, seq, limit, proxy, passwd, timeout);
    }

    /**
     * 获取会话信息（解密）
     * @param sdkId
     * @return
     */
    @RequestMapping("/txchat/getDecryptChatData")
    @ResponseBody
    public Result getChatData(String sdkId,int seq,int limit,String proxy,String passwd,int timeout,String pk){
        Result data = txChatService.getChatData(sdkId, seq, limit, proxy, passwd, timeout);
        if(data.getCode() == Result.Type.SUCCESS.value()){
            JSONObject chatData = null;
            try {
                chatData = JSON.parseObject((String) data.getData());
            } catch (Exception e) {
                System.out.println("json解析错误原数据：" + data.getData());
                throw e;
            }
            JSONArray jsonArray = chatData.getJSONArray("chatdata");
            for (int i = 0; i < jsonArray.size() ; i++) {
                JSONObject chatOne = jsonArray.getJSONObject(i);
                System.out.println("chatOne:"+chatOne.toString());
                Result dcRes = txChatService.decryptData(sdkId,pk,chatOne.getString("encrypt_random_key"),chatOne.getString("encrypt_chat_msg"));
                if(dcRes.getCode() == Result.Type.SUCCESS.value()){
                    try {
                        chatOne.put("body",JSON.parseObject((String) dcRes.getData()));
                    } catch (Exception e) {
                        System.out.println("json解析错误原数据：" + dcRes.getData());
                        throw e;
                    }
                }
            }
            return Result.success("解密数据",chatData.toString());
        }
        return data;
    }

    /**
     * 解密数据
     * @param sdkId
     * @return
     */
    @RequestMapping("/txchat/decryptDataByPk")
    @ResponseBody
    public Result decryptData(String sdkId,String pk, String encryptRandomKey, String encryptChatMsg){
        return txChatService.decryptData(sdkId,pk,encryptRandomKey,encryptChatMsg);
    }

    /**
     * 解密数据
     * @param sdkId
     * @return
     */
    @RequestMapping("/txchat/decryptData")
    @ResponseBody
    public Result decryptData(String sdkId, String encryptKey, String encryptChatMsg){
        return txChatService.decryptData(sdkId,encryptKey,encryptChatMsg);
    }


    /**
     * 获取媒体数据
     * @param sdkId
     * @return
     */
    @RequestMapping("/txchat/getMediaData")
    @ResponseBody
    public Result getMediaData(String sdkId, String indexbuf, String sdkfileid, String proxy, String passwd, int timeout){
        MediaData mediaData =  txChatService.getMediaData(sdkId, indexbuf, sdkfileid, proxy, passwd, timeout);
        return Result.success(mediaData);
    }

    /**
     * 获取媒体数据
     * @param sdkId
     * @return
     */
    @RequestMapping("/txchat/downMediaData")
    @ResponseBody
    public void downMediaData(String sdkId, String filename,long filesize,String fileext,String sdkfileid, String proxy, String passwd, int timeout, HttpServletResponse response){
        OutputStream os = null;
        try {
        boolean flag = false;
            response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            response.setContentLength(Math.toIntExact(filesize));
            response.setContentType(FileUploadUtil.getMIMEType(fileext));
            String indexbuf = null;
            while (!flag){
                MediaData mediaData =  txChatService.getMediaData(sdkId, indexbuf, sdkfileid, proxy, passwd, timeout);
                os = response.getOutputStream();
                os.write(mediaData.getData());
                flag = mediaData.isEnd();
                indexbuf = mediaData.getIndexbuf();
            }
            os.flush();
        } catch (Exception e) {
            logger.error("下载文件断开或发送错误",e);
        } finally {
            IoUtil.close(os);
        }
    }

    public static void main(String[] args) {
        Object ss = "{\"errcode\":0,\"errmsg\":\"ok\",\"chatdata\":[{\"seq\":2654369,\"msgid\":\"10976867661010705669_1703726582326\",\"publickey_ver\":1,\"encrypt_random_key\":\"oR7m11TOAJV+ALdl2q6WDHYiXDnEvRJ7YtpGvGC6huyn/zqHXttmwGZg0EBlA6XGZXW8fVvvcTcAhXRVZNq3/AavEQiDu/7SM2CLVsMr0ZStv/NZmJXliqt5drYhutVel8zH+2t1R/RqIJxvrE2zZvf7vo2goROLrGl4QQazUFj4Zy3ErJIqyPoNp9jU1njM4hQEyTuTEe6W7s/ldN5ZYDi7R6O09SccXsLJLaE1f5o09/mN0dZln/ixcP43kT7Z/ZaR1jCzvrYF6RUTwr+iA7oL0ujgUBVC/CWNcnm/luZPMPWygJ6GaJpUMrVxaWZm5S78+BfR/Z5+H6skQXWy3A==\",\"encrypt_chat_msg\":\"Zr+r9YV4RhhlMkZzrBMf0YJMz0nsYKSlcYlmrYHEmat2swb5ktxBz7TbB2L8iyHpDhvUKQuNIstXaLsFbBnlTtsZwoPJ9novvjh9aZ7cwa7dUBQfuL197iaAe0guN0yehXCsthkJ47zC27MD89rHAMeWtw4IefGonZ8Rktc3W3pvxlluVv8P1bPGJBQo7kLgBaCASUkT5Ab2gG22F7SlMW1ALwl3WvmdxZXfZdM/kHRbOQkREmHYlkvh9ozAgzcSotlzpvM+fif8dNGs/24r85kG/ob4H1TeVVL2EPKfza5souMbH45JC0rqDP51UbLbE2raGQnERqqSQ+kjtmdJRGJpxQOUUceJkIRNZocMWuWTdgEI6EIyIekaFtFZFf3l7lsShFwsZmteeMpwiTiQYjKaBm+22ShZxQrRGI0Ew=TovLYYVrvd4/cs\"}]}.";
        System.out.println(JSON.parseObject((String) ss));
    }
}
